/*DONE* Table [dbo].[<CompanyName>$Item$437dbf0e-84ff-417a-965d-ed2bb9650972]  1/3  Script Date: 28.7.2020 - JI ******/
/*Add prestaging table for all companies in LSInsight$Companies table*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyCounter as INT
DECLARE @i as INT

SET @i = 0
SET @CompanyCounter = (SELECT MAX([Company]) FROM LSInsight$Companies)

WHILE @i <= @CompanyCounter 
BEGIN
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @i)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Item$437dbf0e-84ff-417a-965d-ed2bb9650972]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Item$437dbf0e-84ff-417a-965d-ed2bb9650972](
	[timestamp] [timestamp] NOT NULL,
	[No_] [nvarchar](20) NOT NULL,
	[No_ 2] [nvarchar](20) NOT NULL,
	[Description] [nvarchar](100) NOT NULL,
	[Search Description] [nvarchar](100) NOT NULL,
	[Description 2] [nvarchar](50) NOT NULL,
	[Base Unit of Measure] [nvarchar](10) NOT NULL,
	[Price Unit Conversion] [int] NOT NULL,
	[Type] [int] NOT NULL,
	[Inventory Posting Group] [nvarchar](20) NOT NULL,
	[Shelf No_] [nvarchar](10) NOT NULL,
	[Item Disc_ Group] [nvarchar](20) NOT NULL,
	[Allow Invoice Disc_] [tinyint] NOT NULL,
	[Statistics Group] [int] NOT NULL,
	[Commission Group] [int] NOT NULL,
	[Unit Price] [decimal](38, 20) NOT NULL,
	[Price_Profit Calculation] [int] NOT NULL,
	[Profit _] [decimal](38, 20) NOT NULL,
	[Costing Method] [int] NOT NULL,
	[Unit Cost] [decimal](38, 20) NOT NULL,
	[Standard Cost] [decimal](38, 20) NOT NULL,
	[Last Direct Cost] [decimal](38, 20) NOT NULL,
	[Indirect Cost _] [decimal](38, 20) NOT NULL,
	[Cost is Adjusted] [tinyint] NOT NULL,
	[Allow Online Adjustment] [tinyint] NOT NULL,
	[Vendor No_] [nvarchar](20) NOT NULL,
	[Vendor Item No_] [nvarchar](50) NOT NULL,
	[Lead Time Calculation] [varchar](32) NOT NULL,
	[Reorder Point] [decimal](38, 20) NOT NULL,
	[Maximum Inventory] [decimal](38, 20) NOT NULL,
	[Reorder Quantity] [decimal](38, 20) NOT NULL,
	[Alternative Item No_] [nvarchar](20) NOT NULL,
	[Unit List Price] [decimal](38, 20) NOT NULL,
	[Duty Due _] [decimal](38, 20) NOT NULL,
	[Duty Code] [nvarchar](10) NOT NULL,
	[Gross Weight] [decimal](38, 20) NOT NULL,
	[Net Weight] [decimal](38, 20) NOT NULL,
	[Units per Parcel] [decimal](38, 20) NOT NULL,
	[Unit Volume] [decimal](38, 20) NOT NULL,
	[Durability] [nvarchar](10) NOT NULL,
	[Freight Type] [nvarchar](10) NOT NULL,
	[Tariff No_] [nvarchar](20) NOT NULL,
	[Duty Unit Conversion] [decimal](38, 20) NOT NULL,
	[Country_Region Purchased Code] [nvarchar](10) NOT NULL,
	[Budget Quantity] [decimal](38, 20) NOT NULL,
	[Budgeted Amount] [decimal](38, 20) NOT NULL,
	[Budget Profit] [decimal](38, 20) NOT NULL,
	[Blocked] [tinyint] NOT NULL,
	[Block Reason] [nvarchar](250) NOT NULL,
	[Last DateTime Modified] [datetime] NOT NULL,
	[Last Date Modified] [datetime] NOT NULL,
	[Last Time Modified] [datetime] NOT NULL,
	[Price Includes VAT] [tinyint] NOT NULL,
	[VAT Bus_ Posting Gr_ (Price)] [nvarchar](20) NOT NULL,
	[Gen_ Prod_ Posting Group] [nvarchar](20) NOT NULL,
	[Picture] [uniqueidentifier] NOT NULL,
	[Country_Region of Origin Code] [nvarchar](10) NOT NULL,
	[Automatic Ext_ Texts] [tinyint] NOT NULL,
	[No_ Series] [nvarchar](20) NOT NULL,
	[Tax Group Code] [nvarchar](20) NOT NULL,
	[VAT Prod_ Posting Group] [nvarchar](20) NOT NULL,
	[Reserve] [int] NOT NULL,
	[Global Dimension 1 Code] [nvarchar](20) NOT NULL,
	[Global Dimension 2 Code] [nvarchar](20) NOT NULL,
	[Stockout Warning] [int] NOT NULL,
	[Prevent Negative Inventory] [int] NOT NULL,
	[Application Wksh_ User ID] [nvarchar](128) NOT NULL,
	[Assembly Policy] [int] NOT NULL,
	[GTIN] [nvarchar](14) NOT NULL,
	[Default Deferral Template Code] [nvarchar](10) NOT NULL,
	[Low-Level Code] [int] NOT NULL,
	[Lot Size] [decimal](38, 20) NOT NULL,
	[Serial Nos_] [nvarchar](20) NOT NULL,
	[Last Unit Cost Calc_ Date] [datetime] NOT NULL,
	[Rolled-up Material Cost] [decimal](38, 20) NOT NULL,
	[Rolled-up Capacity Cost] [decimal](38, 20) NOT NULL,
	[Scrap _] [decimal](38, 20) NOT NULL,
	[Inventory Value Zero] [tinyint] NOT NULL,
	[Discrete Order Quantity] [int] NOT NULL,
	[Minimum Order Quantity] [decimal](38, 20) NOT NULL,
	[Maximum Order Quantity] [decimal](38, 20) NOT NULL,
	[Safety Stock Quantity] [decimal](38, 20) NOT NULL,
	[Order Multiple] [decimal](38, 20) NOT NULL,
	[Safety Lead Time] [varchar](32) NOT NULL,
	[Flushing Method] [int] NOT NULL,
	[Replenishment System] [int] NOT NULL,
	[Rounding Precision] [decimal](38, 20) NOT NULL,
	[Sales Unit of Measure] [nvarchar](10) NOT NULL,
	[Purch_ Unit of Measure] [nvarchar](10) NOT NULL,
	[Time Bucket] [varchar](32) NOT NULL,
	[Reordering Policy] [int] NOT NULL,
	[Include Inventory] [tinyint] NOT NULL,
	[Manufacturing Policy] [int] NOT NULL,
	[Rescheduling Period] [varchar](32) NOT NULL,
	[Lot Accumulation Period] [varchar](32) NOT NULL,
	[Dampener Period] [varchar](32) NOT NULL,
	[Dampener Quantity] [decimal](38, 20) NOT NULL,
	[Overflow Level] [decimal](38, 20) NOT NULL,
	[Manufacturer Code] [nvarchar](10) NOT NULL,
	[Item Category Code] [nvarchar](20) NOT NULL,
	[Created From Nonstock Item] [tinyint] NOT NULL,
	[Product Group Code] [nvarchar](10) NOT NULL,
	[Service Item Group] [nvarchar](10) NOT NULL,
	[Item Tracking Code] [nvarchar](10) NOT NULL,
	[Lot Nos_] [nvarchar](20) NOT NULL,
	[Expiration Calculation] [varchar](32) NOT NULL,
	[Warehouse Class Code] [nvarchar](10) NOT NULL,
	[Special Equipment Code] [nvarchar](10) NOT NULL,
	[Put-away Template Code] [nvarchar](10) NOT NULL,
	[Put-away Unit of Measure Code] [nvarchar](10) NOT NULL,
	[Phys Invt Counting Period Code] [nvarchar](10) NOT NULL,
	[Last Counting Period Update] [datetime] NOT NULL,
	[Use Cross-Docking] [tinyint] NOT NULL,
	[Next Counting Start Date] [datetime] NOT NULL,
	[Next Counting End Date] [datetime] NOT NULL,
	[Id] [uniqueidentifier] NOT NULL,
	[Unit of Measure Id] [uniqueidentifier] NOT NULL,
	[Tax Group Id] [uniqueidentifier] NOT NULL,
	[Sales Blocked] [tinyint] NOT NULL,
	[Purchasing Blocked] [tinyint] NOT NULL,
	[Item Category Id] [uniqueidentifier] NOT NULL,
	[Routing No_] [nvarchar](20) NOT NULL,
	[Production BOM No_] [nvarchar](20) NOT NULL,
	[Single-Level Material Cost] [decimal](38, 20) NOT NULL,
	[Single-Level Capacity Cost] [decimal](38, 20) NOT NULL,
	[Single-Level Subcontrd_ Cost] [decimal](38, 20) NOT NULL,
	[Single-Level Cap_ Ovhd Cost] [decimal](38, 20) NOT NULL,
	[Single-Level Mfg_ Ovhd Cost] [decimal](38, 20) NOT NULL,
	[Overhead Rate] [decimal](38, 20) NOT NULL,
	[Rolled-up Subcontracted Cost] [decimal](38, 20) NOT NULL,
	[Rolled-up Mfg_ Ovhd Cost] [decimal](38, 20) NOT NULL,
	[Rolled-up Cap_ Overhead Cost] [decimal](38, 20) NOT NULL,
	[Order Tracking Policy] [int] NOT NULL,
	[Critical] [tinyint] NOT NULL,
	[Common Item No_] [nvarchar](20) NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
	[Purchasing Code] [nvarchar](10) NOT NULL,
	[Over-Receipt Code] [nvarchar](20) NOT NULL,
	[$systemCreatedAt] [datetime] NOT NULL,
	[$systemCreatedBy] [uniqueidentifier] NOT NULL,
	[$systemModifiedAt] [datetime] NOT NULL,
	[$systemModifiedBy] [uniqueidentifier] NOT NULL,
 CONSTRAINT ['+@Company+'$Item$437dbf0e-84ff-417a-965d-ed2bb9650972$Key1] PRIMARY KEY CLUSTERED 
(
	[No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$Item$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END
SET @i = @i+1
END